<?php
        if($_SERVER['REQUEST_METHOD'] == 'POST'){$pr=1;}
        else{$pr=0;}
	// Invoer Controle
	if ($pr)
	{
            $error = 0;
            // naam controle
            if (empty($_POST['firstname'])){$error = 1;}
            // e-mail controle
            if (empty($_POST['email'])){$error = 1;}
            elseif (function_exists('filter_var') && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){$email_error = $error = 1;}
            elseif ($_POST['email'] != $_POST['emailconfirmation']){$emailconfirmation_error = 1;}
            // telefoonnummer controle
            if (empty($_POST['phone'])){$error = 1;}
            elseif (!preg_match("/^0[0-9]{9}$/i", $_POST['phone'])){$phone_error = $error = 1;}
	}

	// Laat forum zien als niet alles (juist) is ingevoerd.
	if (($pr && !empty($error)) || $_SERVER['REQUEST_METHOD'] == 'GET')
	{
		// Het formulier                
		echo '<form method="post" action="' . $_SERVER['REQUEST_URI'] . '"><label for="firstname">Naam:</label><input type="text" id="firstname" name="firstname" value="'.(isset($_POST['firstname'])?htmlspecialchars($_POST['firstname']):'') . '" /><br />';
                if ($pr && empty($_POST['firstname'])){echo '<span style="color:red;">Vul uw naam in</span><br />';}
                else {echo'<br />';}
		echo '<label for="lastname">Achternaam:</label><input type="text" id="lastname" name="lastname" value="'.(isset($_POST['lastname'])?htmlspecialchars($_POST['lastname']):'') . '" /><br />';
                if ($pr && empty($_POST['lastname'])){echo'<span style="color:red;">Vul uw achternaam in</span><br />';}
                else {echo'<br />';}
                echo '<label for="phone">(Mobiele) Telefoon:</label><input type="text" id="phone" name="phone" value="'.(isset($_POST['phone'])?htmlspecialchars($_POST['phone']):'') . '" /><br />';
                if ($pr && empty($_POST['phone'])){echo'<span style="color:red;">Vul uw telefoon in</span><br />';}
                elseif ($pr && !empty($phone_error)){echo '<span style="color:red;">Geef een geldig telefoonnummer op</span><br />';}
                else {echo '<br />';}
		echo '<label for="email">E-mailadres:</label><input type="text" id="email" name="email" value="'.(isset($_POST['email'])?htmlspecialchars($_POST['email']) :'').'" /><br />';
                if ($pr && empty($_POST['email'])){echo'<span style="color:red;">Vul uw e-mailadres in</span><br />';}
                elseif ($pr && !empty($email_error)){echo '<span style="color:red;">Geef een geldig e-mailadres op</span><br />';}
                else {echo'<br />';}
                echo '<label for="emailconfirmation">Bevestig e-mailadres:</label><input type="text" id="emailconfirmation" name="emailconfirmation" value="'.(isset($_POST['emailconfirmation'])?htmlspecialchars($_POST['emailconfirmation']) :'').'" /><br />';
                if ($pr && empty($_POST['$emailconfirmation'])){echo '<span style="color:red;">E-mailadres bevestigen is perplicht</span><br />';}
                elseif ($pr && !empty($emailconfirmation_error)){'<span style="color:red;">De ingevulde e-mail adressen moeten gelijk zijn</span><br />';}
                else {echo'<br />';}
                echo '<label for="lastname">Postcode:</label><input type="text" id="postcode" name="postcode" value="'.(isset($_POST['postcode'])?htmlspecialchars($_POST['postcode']):'') . '" /><br />';
                echo '<input type="submit" name="submit" value=" Versturen " /></form>';
        }
	// Uitvoer als alles (juist) is ingevoerd.
	else
	{
		echo '<h1>Succes</h1>
		<p>Het formulier is goed ingevuld.</p>';
	}
?>